#=========================== begin_copyright_notice ============================
#
# Copyright (C) 2017-2021 Intel Corporation
#
# SPDX-License-Identifier: MIT
#
#============================ end_copyright_notice =============================

FILE(GLOB_RECURSE CIF_HEADERS RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}/.." "*.h")

if(MSVC OR MINGW)
  SET(OS "win")
else()
  SET(OS "lin")
endif(MSVC OR MINGW)

MACRO(MAKE_ABSOLUTE_PATHS ROOT_DIR RELATIVE_PATHS_LIST DST_ABSOLUTE_PATHS_LIST)
  SET(RET_LIST "")
  FOREACH(IT ${RELATIVE_PATHS_LIST})
    SET(ABS_PATH ${ROOT_DIR}/${IT})
    SET(RET_LIST "${RET_LIST};${ABS_PATH}")
  ENDFOREACH()

  SET(${DST_ABSOLUTE_PATHS_LIST} ${RET_LIST})
ENDMACRO(MAKE_ABSOLUTE_PATHS)

MACRO(SET_CIF_FILTERS_SINGLE FILES_RELATIVE BASE_DIR FILTER_BASE)
    SET(ROOT_PATH "${BASE_DIR}cif/")
    GET_FILENAME_COMPONENT(ROOT_PATH_ABSOLUTE "${ROOT_PATH}" ABSOLUTE)
    FOREACH(IT ${FILES_RELATIVE})
        GET_FILENAME_COMPONENT(PATH_WITHOUT_FILENAME ${IT} DIRECTORY)
        GET_FILENAME_COMPONENT(PATH_WITHOUT_FILENAME_ABSOLUTE "${PATH_WITHOUT_FILENAME}" ABSOLUTE)
        STRING(REPLACE "${ROOT_PATH_ABSOLUTE}" "" PARTIAL_PATH ${PATH_WITHOUT_FILENAME_ABSOLUTE})
        STRING(REPLACE "/" "\\" FILTER ${PARTIAL_PATH})
        SET(FILTER "${FILTER_BASE}${FILTER}")
        SOURCE_GROUP("${FILTER}" FILES "${IT}")
    ENDFOREACH(IT)
ENDMACRO(SET_CIF_FILTERS_SINGLE)

MACRO(SET_CIF_FILTERS SRC_CIF_SOURCES_RELATIVE SRC_CIF_HEADERS_RELATIVE BASE_DIR)
    SET_CIF_FILTERS_SINGLE("${SRC_CIF_SOURCES_RELATIVE}" "${BASE_DIR}" "cif\\\\sources\\\\")
    SET_CIF_FILTERS_SINGLE("${SRC_CIF_HEADERS_RELATIVE}" "${BASE_DIR}" "cif\\\\headers\\\\")
ENDMACRO(SET_CIF_FILTERS)

MACRO(CONCAT_LIST DST_LIST SRC_LIST SRC_PREFIX)
    SET(RET_LIST "")
    FOREACH(IT ${SRC_LIST})
        SET(RET_LIST "${RET_LIST};${SRC_PREFIX}${IT}")
    ENDFOREACH(IT)
    SET(${DST_LIST} ${RET_LIST})
ENDMACRO(CONCAT_LIST)

MACRO(GET_CIF_FILES_RELATIVE DST_CIF_SOURCES_EXPORT DST_CIF_SOURCES_IMPORT DST_CIF_HEADERS BASE_DIR)
    CONCAT_LIST(RET_CIF_SOURCES_EXPORT "${CIF_SOURCES_EXPORT}" "${BASE_DIR}")
    CONCAT_LIST(RET_CIF_SOURCES_IMPORT "${CIF_SOURCES_IMPORT}" "${BASE_DIR}")
    CONCAT_LIST(RET_CIF_HEADERS "${CIF_HEADERS}" "${BASE_DIR}")
    SET(${DST_CIF_SOURCES_EXPORT} ${RET_CIF_SOURCES_EXPORT})
    SET(${DST_CIF_SOURCES_IMPORT} ${RET_CIF_SOURCES_IMPORT})
    SET(${DST_CIF_HEADERS} ${RET_CIF_HEADERS})
ENDMACRO(GET_CIF_FILES_RELATIVE)

SET(CIF_BUILTINS_SOURCES cif/builtins/memory/buffer/impl/buffer_impl.cpp

   )

SET(CIF_SOURCES_EXPORT "${CIF_BUILTINS_SOURCES}"
                       cif/builtins/builtins_registry.cpp
                       cif/export/cif_main.cpp
                       cif/export/registry.cpp
    )

SET(CIF_SOURCES_IMPORT cif/import/cif_main.cpp
                       cif/os/${OS}/${OS}_library_handle.cpp
    )

SET(CIF_SOURCES "${CIF_SOURCES_IMPORT};${CIF_SOURCES_EXPORT}")

MAKE_ABSOLUTE_PATHS("${CMAKE_CURRENT_SOURCE_DIR}/.." "${CIF_SOURCES_EXPORT}" CIF_SOURCES_EXPORT_ABSOLUTE_PATH)
MAKE_ABSOLUTE_PATHS("${CMAKE_CURRENT_SOURCE_DIR}/.." "${CIF_SOURCES_IMPORT}" CIF_SOURCES_IMPORT_ABSOLUTE_PATH)
SET(CIF_SOURCES_ABSOLUTE_PATH "${CIF_SOURCES_EXPORT_ABSOLUTE_PATH};${CIF_SOURCES_IMPORT_ABSOLUTE_PATH}")
MAKE_ABSOLUTE_PATHS("${CMAKE_CURRENT_SOURCE_DIR}/.." "${CIF_HEADERS}" CIF_HEADERS_ABSOLUTE_PATH)

# Export these to parent CMake
SET(CIF_HEADERS ${CIF_HEADERS} PARENT_SCOPE)
SET(CIF_SOURCES_EXPORT ${CIF_SOURCES_EXPORT} PARENT_SCOPE)
SET(CIF_SOURCES_IMPORT ${CIF_SOURCES_IMPORT} PARENT_SCOPE)
SET(CIF_SOURCES ${CIF_SOURCES} PARENT_SCOPE)
SET(CIF_SOURCES_EXPORT_ABSOLUTE_PATH ${CIF_SOURCES_EXPORT_ABSOLUTE_PATH} PARENT_SCOPE)
SET(CIF_SOURCES_IMPORT_ABSOLUTE_PATH ${CIF_SOURCES_IMPORT_ABSOLUTE_PATH} PARENT_SCOPE)
SET(CIF_SOURCES_ABSOLUTE_PATH ${CIF_SOURCES_ABSOLUTE_PATH} PARENT_SCOPE)
SET(CIF_HEADERS_ABSOLUTE_PATH ${CIF_HEADERS_ABSOLUTE_PATH} PARENT_SCOPE)

SET(CIF_FOUND TRUE PARENT_SCOPE)
SET(CIF_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/.." PARENT_SCOPE)
